﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Restaurant.Modules.SYST.Utility;
using Sector7.Restaurant.CoreType.Enums;
using Sector7.Restaurant.Modules.SYST.LocalService;
using System.Collections;
using Sector7.Framework.Enums;
using Sector7.Framework.Core;
using Sector7.Framework.Utility;

namespace Sector7.Restaurant.Modules.SYST.Windows
{
    /// <summary>
    /// MetaDataSummary.xaml 的交互逻辑
    /// </summary>
    public partial class MetaDataSummaryWindow : UserControl
    {
        private List<RS_Unit> _units = null;
        private List<RS_MetadataType> _types = null;

        public MetaDataSummaryWindow()
        {
            InitializeComponent();
        }

        private void InitializeData()
        {
            _units = Aide.LocalClient.Invoke<List<RS_Unit>>(
                service => service.GetAllUnits());
            ClientUtil.LoadItemsSource(dtgUnit, _units);

            _types = Aide.LocalClient.Invoke<List<RS_MetadataType>>(
                service => service.GetAllMetadataTypes(true));
            ClientUtil.LoadItemsSource(dtgType, _types);
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            InitializeData();
        }

        private void btnAddUnit_Click(object sender, RoutedEventArgs e)
        {
            AddUnitWindow addUnitWindow = new AddUnitWindow();
            addUnitWindow.InitializeWindow(CrudType.Create);
            addUnitWindow.AddSuccess += new EventHandler(addUnitWindow_AddSuccess);

            addUnitWindow.ShowDialog();
        }

        private void addUnitWindow_AddSuccess(object sender, EventArgs e)
        {
            AddUnitWindow addUnitWindow = sender as AddUnitWindow;
            RS_Unit newUnit = addUnitWindow.Unit;

            _units.Add(newUnit);

            ClientUtil.LoadSelectedItem(dtgUnit, newUnit);

            addUnitWindow.ResetWindow();
        }

        private void btnModifyUnit_Click(object sender, RoutedEventArgs e)
        {
            AddUnitWindow modifyUnitWindow = new AddUnitWindow();
            modifyUnitWindow.Unit = dtgUnit.SelectedItem as RS_Unit;
            modifyUnitWindow.InitializeWindow(CrudType.Update);
            modifyUnitWindow.AddSuccess += new EventHandler(modifyUnitWindow_AddSuccess);
            
            modifyUnitWindow.ShowDialog();
        }

        private void modifyUnitWindow_AddSuccess(object sender, EventArgs e)
        {
            AddUnitWindow modifyUnitWindow = sender as AddUnitWindow;
            RS_Unit newUnit = modifyUnitWindow.Unit;

            _units.Find(delegate(RS_Unit unit)
            {
                if (unit.UnitId == newUnit.UnitId)
                {
                    unit = newUnit;
                    return true;
                }
                else
                {
                    return false;
                }
            });

            ClientUtil.LoadSelectedItem(dtgUnit, newUnit);
        }

        private void btnDeleteUnit_Click(object sender, RoutedEventArgs e)
        {

        }

        private void dtgType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RS_MetadataType type = dtgType.SelectedItem as RS_MetadataType;
            if (null != type)
                ClientUtil.LoadItemsSource(dtgMetadata, type.Metadatas);
        }

        private void DataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            DataGrid dtg = sender as DataGrid;
            if (dtg.HasItems)
                dtg.SelectedIndex = 0;
        }

        private void btnAddType_Click(object sender, RoutedEventArgs e)
        {
            AddMetadataTypeWindow addTypeWindow = new AddMetadataTypeWindow();
            addTypeWindow.InitializeWindow(CrudType.Create);
            addTypeWindow.AddSuccess += new EventHandler(addTypeWindow_AddSuccess);

            addTypeWindow.ShowDialog();
        }

        private void addTypeWindow_AddSuccess(object sender, EventArgs e)
        {
            AddMetadataTypeWindow addTypeWindow = sender as AddMetadataTypeWindow;
            RS_MetadataType newType = addTypeWindow.MetadataType;

            _types.Add(newType);

            ClientUtil.LoadSelectedItem(dtgType, newType);

            addTypeWindow.ResetWindow();
        }

        private void btnModifyType_Click(object sender, RoutedEventArgs e)
        {
            AddMetadataTypeWindow modifyTypeWindow = new AddMetadataTypeWindow();
            modifyTypeWindow.MetadataType = dtgType.SelectedItem as RS_MetadataType;
            modifyTypeWindow.InitializeWindow(CrudType.Update);
            modifyTypeWindow.AddSuccess += new EventHandler(modifyTypeWindow_AddSuccess);

            modifyTypeWindow.ShowDialog();
        }

        void modifyTypeWindow_AddSuccess(object sender, EventArgs e)
        {
            AddMetadataTypeWindow modifyTypeWindow = sender as AddMetadataTypeWindow;
            RS_MetadataType newType = modifyTypeWindow.MetadataType;

            _types.Find(delegate(RS_MetadataType type)
            {
                if (type.TypeId == newType.TypeId)
                {
                    type = newType;
                    return true;
                }
                else
                {
                    return false;
                }
            });

            ClientUtil.LoadSelectedItem(dtgType, newType);
        }

        private void btnDeleteType_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btnAddMetadata_Click(object sender, RoutedEventArgs e)
        {
            RS_MetadataType selectedType = dtgType.SelectedItem as RS_MetadataType;

            if (null != selectedType)
            {
                AddMetadataWindow addMetadataWindow = new AddMetadataWindow(selectedType, _types);
                addMetadataWindow.InitializeWindow(CrudType.Create);
                addMetadataWindow.AddSuccess += new EventHandler(addMetadataWindow_AddSuccess);

                addMetadataWindow.ShowDialog();
            }
        }

        private void addMetadataWindow_AddSuccess(object sender, EventArgs e)
        {
            AddMetadataWindow addMetadataWindow = sender as AddMetadataWindow;

            RS_Metadata newMetadata = addMetadataWindow.Metadata;
            _types.Find(delegate(RS_MetadataType type)
            {
                if (type.TypeId == newMetadata.TypeId)
                {
                    type.Metadatas.Add(newMetadata);
                    return true;
                }
                else
                {
                    return false;
                }
            });

            ClientUtil.LoadSelectedItem(dtgMetadata, newMetadata);

            addMetadataWindow.ResetWindow();
        }

        private void btnModifyMetadata_Click(object sender, RoutedEventArgs e)
        {
            RS_MetadataType selectedType = dtgType.SelectedItem as RS_MetadataType;

            AddMetadataWindow modifyMetadataWindow = new AddMetadataWindow(selectedType, _types);
            modifyMetadataWindow.Metadata = dtgMetadata.SelectedItem as RS_Metadata;
            modifyMetadataWindow.InitializeWindow(CrudType.Update);
            modifyMetadataWindow.AddSuccess += new EventHandler(modifyMetadataWindow_AddSuccess);

            modifyMetadataWindow.ShowDialog();
        }

        void modifyMetadataWindow_AddSuccess(object sender, EventArgs e)
        {
            var modifyMetadataWindow = sender as AddMetadataWindow;

            RS_Metadata newMetadata = modifyMetadataWindow.Metadata;
            _types.Find(delegate(RS_MetadataType type)
            {
                if (type.TypeId == newMetadata.TypeId)
                {
                    type.Metadatas.Find(delegate(RS_Metadata metadata)
                    {
                        if (metadata.Id == newMetadata.Id)
                        {
                            metadata = newMetadata;
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    });

                    return true;
                }
                else
                {
                    return false;
                }
            });

            ClientUtil.LoadSelectedItem(dtgMetadata, newMetadata);
        }

        private void btnDeleteMetadata_Click(object sender, RoutedEventArgs e)
        {

        }
    }
}
